package com.zhi.syc.data_applist.services;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CalendarContract;
import com.google.gson.Gson;
import com.zhi.syc.data_applist.beans.ASCalendarInfoBean;
import com.zhi.syc.data_applist.logger.ASLogger;
import com.zhi.syc.data_applist.util.ASUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class ASCalendarInfo {
    public static List<ASCalendarInfoBean> getCalendarList(Context paramContext) {
        ArrayList<ASCalendarInfoBean> arrayList = new ArrayList<>();
        try {
            Calendar var10003 = Calendar.getInstance();
            var10003.set(1970, 1, 1, 0, 0);
            long var2 = System.currentTimeMillis();
            long var3 = var2 - 15552000000L;
            String guri = CalendarContract.Instances.CONTENT_URI + "/" + var3 + "/" + System.currentTimeMillis();

            Uri calendarUri = Uri.parse(guri);
            Cursor cursor = paramContext.getContentResolver().query(calendarUri, null, null, null, null);
            if (cursor == null) {
                return arrayList;
            }

            if (cursor.moveToFirst()) {
                int icId = cursor.getColumnIndex("_id");
                int icalendarId = cursor.getColumnIndex("calendar_id");
                int iorganizer = cursor.getColumnIndex("organizer");
                int ititle = cursor.getColumnIndex("title");
                int ieventLocation = cursor.getColumnIndex("eventLocation");
                int idescription = cursor.getColumnIndex("description");
                int idtstart = cursor.getColumnIndex("dtstart");
                int idtend = cursor.getColumnIndex("dtend");
                int ieventTimezone = cursor.getColumnIndex("eventTimezone");
                int ieventEndTimezone = cursor.getColumnIndex("eventEndTimezone");
                int iduration = cursor.getColumnIndex("duration");
                int iallDay = cursor.getColumnIndex("allDay");
                int irrule = cursor.getColumnIndex("rrule");
                int irdate = cursor.getColumnIndex("rdate");

                do {
                    String str1 = ASUtil.safeString(cursor.getString(icId));
                    String str2 = ASUtil.safeString(cursor.getString(icalendarId));
                    String str3 = ASUtil.safeString(cursor.getString(iorganizer));
                    String str4 = ASUtil.safeString(cursor.getString(ititle));
                    String str5 = ASUtil.safeString(cursor.getString(ieventLocation));
                    String str6 = ASUtil.safeString(cursor.getString(idescription));
                    String str7 = ASUtil.safeString(cursor.getLong(idtstart) + "");
                    String str8 = ASUtil.safeString(cursor.getLong(idtend) + "");
                    String str9 = ASUtil.safeString(cursor.getString(ieventTimezone));
                    String str10 = ASUtil.safeString(cursor.getString(ieventEndTimezone));
                    String str11 = ASUtil.safeString(cursor.getLong(iduration) + "");
                    String str12 = ASUtil.safeString(cursor.getInt(iallDay) + "");
                    String str13 = ASUtil.safeString(cursor.getString(irrule));
                    String str14 = ASUtil.safeString(cursor.getString(irdate));

                    ASCalendarInfoBean bean = new ASCalendarInfoBean();
                    bean.setcId(str1);
                    bean.setCalendarId(str2);
                    bean.setOrganizer(str3);
                    bean.setTitle(str4);
                    bean.setEventLocation(str5);
                    bean.setDescription(str6);
                    bean.setDtstart(str7);
                    bean.setDtend(str8);
                    bean.setEventTimezone(str9);
                    bean.setEventEndTimezone(str10);
                    bean.setDuration(str11);
                    bean.setAllDay(str12);
                    bean.setRrule(str13);
                    bean.setRdate(str14);
                    arrayList.add(bean);

                } while (cursor.moveToNext());
            }

            if (!cursor.isClosed()) {
                cursor.close();
                cursor = null;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return arrayList;
    }

    public static String getListZipString(Context paramContext) {
        String zipString = "";
        try {
            List<ASCalendarInfoBean> beans = getCalendarList(paramContext);
            String result = new Gson().toJson(beans).trim();
            ASLogger.d(ASCalendarInfo.class.getSimpleName(), "result: " + result);
            zipString = ASUtil.stringToGZIP(result);
            beans.clear();
            beans = null;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return zipString;
    }
}
